package com.example.chirdrenserver.controller;

import com.example.chirdrenserver.Pojo.dto.EmailDTO;
import com.example.chirdrenserver.Pojo.dto.RegisterDTO;
import com.example.chirdrenserver.Pojo.dto.UserLoginDTO;
import com.example.chirdrenserver.Pojo.entity.User;
import com.example.chirdrenserver.Pojo.vo.UserLoginVO;
import com.example.chirdrenserver.Service.CaptchaService;
import com.example.chirdrenserver.Service.EmailService;
import com.example.chirdrenserver.Service.UserLoginService.UserLoginSerivce;
import com.example.chirdrenserver.Utils.JwtUtils;
import com.example.chirdrenserver.Utils.Result;
import com.example.chirdrenserver.properties.UserJwtProperties;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登录
 */
@RestController
@RequestMapping("/user/login")
@Slf4j
public class UserLoginController {
    @Autowired
    private UserLoginSerivce userLoginSerivce;
    @Autowired
    private UserJwtProperties jwtProperties;

    @Autowired
    EmailService emailService;

    @Autowired
    private CaptchaService captchaService;

    @PostMapping
    public Result userLogin(@RequestBody UserLoginDTO userLoginDTO) {
        User user = userLoginSerivce.userLogin(userLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        String token = JwtUtils.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        Map<String,Object> map = new HashMap<>();
        map.put(jwtProperties.getUserTokenName(), token);
        return Result.success(map);
    }


    /**
     * 获得图片验证码
     * @param uuId
     * @param response
     * @throws IOException
     */

    @GetMapping("/captcha/{uuId}")
    public void SignUpCaptcha(@PathVariable String uuId, HttpServletResponse response) throws IOException {
        captchaService.createCaptcha(uuId, response);
    }

    /**
     * 获得邮箱验证码
     * @param emailDTO
     * @return
     */
    @PostMapping("/getcode")
    public Result SignUpCaptcha(@RequestBody @Validated EmailDTO emailDTO) {
        log.info("{}",emailDTO);
        userLoginSerivce.validate(emailDTO);
        return  Result.success("发送成功");
    }

    /**
     * 检测邮箱验证码
     * @param email
     * @param code
     * @return
     */
    @PostMapping("/check")
    public Result check(String email,Integer code){
        emailService.checkCode(email,code);
        return Result.success("验证码成功");
    }

    /**
     * 注册
     * @param registerDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Validated RegisterDTO registerDTO) throws Exception {
        if (!userLoginSerivce.register(registerDTO)) {
            return Result.error("注册失败,验证码错误");
        }
        return Result.success("注册成功");
    }
}
